<?php
namespace Admin\Controller;

use Think\Controller;

class HotEventsController extends CommonController
{
    /*
	 * 方法: index
	 * 功能：热门活动列表
	**/
    public function index()
    {
        $ptitle='热门活动管理';
        $title='首页';
        $row = 10;
        $page = $_GET['p'];
        if ($page == '') {
            $page = 1;
        }
        $where = array(
            'status' => 1,
        );
        $hot_events_list = M('hot_events')
            ->where($where)
            ->page($page, $row)
            ->order('sort asc')
            ->select();
        $hot_events_num =  M('hot_events')
            ->where($where)
            ->count();
        $Page = new \Think\Page($hot_events_num, $row);
        $show = $Page->show();
        $this->assign('ptitle', $ptitle);
        $this->assign('title', $title);
        $this->assign('page', $show);
        $this->assign('hot_events_list', $hot_events_list);
        $this->display();
    }

    /*
    * 方法: add
    * 功能：热门活动添加
   **/
    public function add()
    {
        if(!IS_POST){
            $ptitle='热门活动管理';
            $title='添加';
            $this->assign('ptitle', $ptitle);
            $this->assign('title', $title);
            $this->display();
        }else{
            $data['events_name'] = I('events_name');
            $data['events_pic'] = I('events_pic');
            $data['events_describe'] = I('events_describe');
            $data['status'] = I('status');
            $data['sort'] = I('sort');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            $addResult = M('hot_events')->add($data);
            if($addResult){
                echo 1;
            }else{
                echo 0;
            }
        }
    }

    /*
    * 方法: edit
    * 功能：热门活动修改
    * 参数：job_id，职位ID
   **/
    public function edit()
    {
        if(!IS_POST){
            $ptitle = '热门活动管理';
            $title = '修改';
            $events_id = I('events_id');
            //查询该文章分类信息
            $hot_events_info = M('hot_events')
                ->where(array('events_id' => $events_id))
                ->find();
            //查询所有一级分类
            $this->assign('ptitle', $ptitle);
            $this->assign('title', $title);
            $this->assign('hot_events_info', $hot_events_info);
            $this->assign('events_id', $events_id);
            $this->display();
        }else{
            $data['events_id'] = I('events_id');
            $data['events_name'] = I('events_name');
            $data['events_pic'] = I('events_pic');
            $data['events_describe'] = I('events_describe');
            $data['status'] = I('status');
            $data['order'] = I('order');
            $data['sort'] = I('sort');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            $editResult = M('hot_events')->save($data);
            if($editResult !== false){
                echo 1;
            }else{
                echo 0;
            }
        }
    }

    /*
   * 方法: edit
   * 功能：热门活动删除
   * 参数：events_id，热门活动ID
  **/
    public function del()
    {
        $ids = I('ids');
        if(is_array($ids))
        {
            $ids = implode(',',$ids);
        }
        $map = array(
            'events_id' => array(
                'in',"$ids"
        ));

        if (M('hot_events ')->where($map)->delete() !== false) {
            $this->success('热门活动删除成功！');
        } else {
            $this->error('热门活动删除失败！');
        }
    }
}